Column

Total Diapers Per Day

Total Both Diapers per Day

Total Wet Diapers per Day

Total Dirty Diapers per Day

Total Feedings per Day

Column

Diaper Type Plot

Daily Total Diaper Statistics

Daily Wet Diaper Statistics

Daily Dirty Diaper Statistics

Daily Both Diaper Statistics

Daily Feedings Statistics

---
title: "Fiona Data"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
    vertical_layout: scroll
    theme: spacelab
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(rio)
library(here)
library(colorblindr)
library(gghighlight)
library(forcats)
library(ggrepel)
library(gt)
library(knitr)
library(kableExtra)
library(reactable)
library(plotly)

opts_chunk$set(echo = FALSE,
               fig.width = 5,
               fig.height = 6)

theme_set(theme_minimal(base_size = 10))

fiona <- import(here("data", "fiona_all.xlsx"),
                setclass = "tbl_df") %>% 
  janitor::clean_names()

diaper <- import(here("data", "fiona_diaper.xlsx"),
               setclass = "tbl_df") %>% 
  janitor::clean_names() 

head(diaper)

feed <- import(here("data", "fiona_feed.xlsx"),
               setclass = "tbl_df") %>% 
  janitor::clean_names() 

head(feed)

#fiona <- full_join(diaper, feed)

fiona_1 <- fiona %>% 
  rename("Wet" = total_wet,
         "Dirty" = total_dirty,
         "Both" = total_both)

head(fiona_1)

fiona_tidy <- fiona_1 %>% 
  pivot_longer(
    c(4:6),
    names_to = "diaper_type",
    values_to = "total"
  )

head(fiona)
head(fiona_tidy)

str(fiona)

fiona$date <- as.Date(fiona$date, format = "%Y-%m-%d")

str(fiona)
```


Column {.tabset data-width=750}
-----------------------------------------------------------------------

### Total Diapers Per Day

```{r diaper plot, fig.width=7}
line_plot <- ggplot(fiona, aes(date, total_diaper)) +
  geom_line(lwd = 1.6,
            color = "gray80") +
  scale_x_date(date_breaks = "3 months",
               date_labels = "%b %Y") +
  scale_y_continuous(limits = c(0, 16),
                     breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16)) +
  geom_area(fill = "cornflowerblue",
            alpha = 0.3) +
  geom_point(size = 2, 
             color = "magenta") +
  geom_text_repel(aes(label = total_diaper),
                  size = 4) +
  theme(plot.title = element_text(color = "black", 
                                  size = 12, 
                                  face = "bold", 
                                  hjust = 0.5),
        axis.text = element_text(size = 10),
        axis.title=element_text(size=10)) +
  labs(x = "Date",
       y = "Total",
       title = "Number of Diapers per Day")

ggplotly(line_plot)
```

### Total Both Diapers per Day 
```{r both diaper plot data clean, include=FALSE}
head(fiona)

both_diapers <- fiona %>% 
  select(date, total_both)

```

```{r both diaper plot, fig.width=7}
both_plot <- ggplot(both_diapers, aes(date, total_both)) +
  geom_line(lwd = 1.6,
            color = "gray80") +
  scale_x_date(date_breaks = "3 months",
               date_labels = "%b %Y") +
  scale_y_continuous(limits = c(0, 16),
                     breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16)) +
  geom_area(fill = "cornflowerblue",
            alpha = 0.3) +
  geom_point(size = 2, 
             color = "magenta") +
  geom_text_repel(aes(label = total_both),
                  size = 4) +
  theme(plot.title = element_text(color = "black", 
                                  size = 12, 
                                  face = "bold", 
                                  hjust = 0.5),
        axis.text = element_text(size = 10),
        axis.title=element_text(size=10)) +
  labs(x = "Date",
       y = "Total",
       title = "Number of Both Diapers per Day")

ggplotly(both_plot)
```


### Total Wet Diapers per Day 
```{r wet diaper plot data clean, include=FALSE}
head(fiona)

wet_diapers <- fiona %>% 
  select(date, total_wet)

```

```{r wet diaper plot, fig.width=7}
wet_plot <- ggplot(wet_diapers, aes(date, total_wet)) +
  geom_line(lwd = 1.6,
            color = "gray80") +
  scale_x_date(date_breaks = "3 months",
               date_labels = "%b %Y") +
  scale_y_continuous(limits = c(0, 16),
                     breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16)) +
  geom_area(fill = "cornflowerblue",
            alpha = 0.3) +
  geom_point(size = 2, 
             color = "magenta") +
  geom_text_repel(aes(label = total_wet),
                  size = 4) +
  theme(plot.title = element_text(color = "black", 
                                  size = 12, 
                                  face = "bold", 
                                  hjust = 0.5),
        axis.text = element_text(size = 10),
        axis.title=element_text(size=10)) +
  labs(x = "Date",
       y = "Total",
       title = "Number of Wet Diapers per Day")

ggplotly(wet_plot)
```


### Total Dirty Diapers per Day 
```{r dirty diaper plot data clean, include=FALSE}
head(fiona)

dirty_diapers <- fiona %>% 
  select(date, total_dirty)

```

```{r dirty diaper plot, fig.width=7}
dirty_plot <- ggplot(dirty_diapers, aes(date, total_dirty)) +
  geom_line(lwd = 1.6,
            color = "gray80") +
  scale_x_date(date_breaks = "3 months",
               date_labels = "%b %Y") +
  scale_y_continuous(limits = c(0, 16),
                     breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16)) +
  geom_area(fill = "cornflowerblue",
            alpha = 0.3) +
  geom_point(size = 2, 
             color = "magenta") +
  geom_text_repel(aes(label = total_dirty),
                  size = 4) +
  theme(plot.title = element_text(color = "black", 
                                  size = 12, 
                                  face = "bold", 
                                  hjust = 0.5),
        axis.text = element_text(size = 10),
        axis.title=element_text(size=10)) +
  labs(x = "Date",
       y = "Total",
       title = "Number of Dirty Diapers per Day")

ggplotly(dirty_plot)
```

### Total Feedings per Day 
```{r, include=FALSE}
head(fiona)

feedings <- fiona %>% 
  select(date, feedings)

```

```{r, fig.width=7}
feedings_plot <- ggplot(feedings, aes(date, feedings)) +
  geom_line(lwd = 1.6,
            color = "gray80") +
  scale_x_date(date_breaks = "3 months",
               date_labels = "%b %Y") +
  scale_y_continuous(limits = c(0, 16),
                     breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16)) +
  geom_area(fill = "cornflowerblue",
            alpha = 0.3) +
  geom_point(size = 2, 
             color = "magenta") +
  geom_text_repel(aes(label = feedings),
                  size = 4) +
  theme(plot.title = element_text(color = "black", 
                                  size = 12, 
                                  face = "bold", 
                                  hjust = 0.5),
        axis.text = element_text(size = 10),
        axis.title=element_text(size=10)) +
  labs(x = "Date",
       y = "Total",
       title = "Number of Feedings per Day")

ggplotly(feedings_plot)
```

Column {.tabset data-width=350}
-----------------------------------------------------------------------

### Diaper Type Plot
```{r diaper type data cleaning, include=FALSE}
head(fiona_tidy)
head(fiona_1)

fiona_tidy %>% 
  count(total)

diaper_type <- fiona_tidy$diaper_type
diaper_total <- sum(fiona_tidy$total)

diapers <- tibble(diaper_type, diaper_total)


Wet <- sum(fiona_1$Wet)
Dirty <- sum(fiona_1$Dirty)
Both <- sum(fiona_1$Both)

diapers_2 <- tibble(Wet, Dirty, Both)

diaper_tidy <- diapers_2 %>% 
  pivot_longer(
    c(1:3),
    names_to = "diaper_type",
    values_to = "total"
  )

diaper_position <- c("Wet", "Dirty", "Both")
```

```{r diaper type}
dplot <- ggplot(diaper_tidy, aes(diaper_type, total)) +
  geom_col(fill = "blue", 
           alpha = 0.7) +
  scale_x_discrete(limits = diaper_position) +
  scale_y_continuous(limits = c(0, 1600),
                     breaks = c(0, 200, 400, 600, 800, 1000, 1200, 1400, 1600)) +
  geom_text(aes(label = scales::comma(total)),
             position = position_stack(vjust = 0.5),
            color = "white",
            size = 5) +
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_line(color = "gray80")) +
  theme(plot.title = element_text(color = "black", size = 12, face = "bold", hjust = 0.5),
        axis.text = element_text(size = 10),
        axis.title=element_text(size=10),
        strip.text = element_text(size = 10)) +
  labs(x = "",
       y = "Total",
       title = "Total Number of Diaper Type")

ggplotly(dplot)

```

### Daily Total Diaper Statistics

```{r table summary clean, include=FALSE}
head(fiona)

diaper_react <- function(df, var) {
    df %>% 
      summarize(`Daily Mean` = mean({{var}}),
                `Daily SD` = sd({{var}}),
                `Daily Min` = min({{var}}),
                `Daily Max` = max({{var}}),
                Total = sum({{var}})) %>% 
      mutate_if(is.numeric, round, 2) %>% 
      reactable(columns = list(
        `Daily Mean` = colDef(format = colFormat(separators = TRUE, suffix = " diapers")),
        `Daily SD` = colDef(format = colFormat(separators = TRUE, suffix = " diapers")),
        `Daily Min` = colDef(format = colFormat(separators = TRUE, suffix = " diapers")),
        `Daily Max` = colDef(format = colFormat(separators = TRUE, suffix = " diapers")),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " diapers"))),
  height = 500,
  striped = TRUE,
  compact = TRUE,
  outlined = TRUE,
  bordered = TRUE)
}

diaper_react(fiona, total_diaper)
diaper_react(fiona, total_wet)
diaper_react(fiona, total_dirty)
diaper_react(fiona, total_both)

feedings_react <- function(df, var) {
    df %>% 
      summarize(`Daily Mean` = mean({{var}}),
                `Daily SD` = sd({{var}}),
                `Daily Min` = min({{var}}),
                `Daily Max` = max({{var}}),
                Total = sum({{var}})) %>% 
      mutate_if(is.numeric, round, 2) %>% 
      reactable(columns = list(
        `Daily Mean` = colDef(format = colFormat(separators = TRUE, suffix = " feedings")),
        `Daily SD` = colDef(format = colFormat(separators = TRUE, suffix = " feedings")),
        `Daily Min` = colDef(format = colFormat(separators = TRUE, suffix = " feedings")),
        `Daily Max` = colDef(format = colFormat(separators = TRUE, suffix = " feedings")),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " feedings"))),
  height = 500,
  striped = TRUE,
  compact = TRUE,
  outlined = TRUE,
  bordered = TRUE)
}

feedings_react(fiona, feedings)

```

```{r}
diaper_react(fiona, total_diaper)
```

### Daily Wet Diaper Statistics
```{r}
diaper_react(fiona, total_wet)
```

### Daily Dirty Diaper Statistics
```{r}
diaper_react(fiona, total_dirty)
```

### Daily Both Diaper Statistics
```{r}
diaper_react(fiona, total_both)
```

### Daily Feedings Statistics 
```{r}
feedings_react(fiona, feedings)
```